import {
    updateStorage
} from '@/utils'
import request from '@/utils/request'

let userInfo = localStorage.getItem('userInfo')
try {
    userInfo = JSON.parse(userInfo) || {}
} catch (err) {
    userInfo = {}
}

export default {
    // 启用命名空间
    namespaced: true,
    state: {
        userInfo,
    },
    getters: {
        isLogin(state
            // ,getters,rootState,rootGetters
        ) {
            // state： 当前模块的state
            // getters: 当前模块的getters
            // rootState: 全局状态（包含所有模块的状态）
            // rootGetters: 全局getters（包含所有模块的getters）
            // console.log('user->state,getters,rootState,rootGetters',state,getters,rootState,rootGetters)
            return !!state.userInfo.Authorization
        }
    },
    mutations: {
        login(state, payload) {
            state.userInfo = payload
            updateStorage('userInfo', payload)
        },
        logout(state) {

            state.userInfo = {}
            localStorage.removeItem('userInfo')
            localStorage.removeItem('RouterList')
        }
    },
    actions: {
        async login(context, payload) {
            // context={state,getters,commit,dispatch}
            console.log('context', context);
            const {
                data
            } = await request.post('/studentLogin/login', payload)

            if (data.code === 200) {
                // console.log("1212121212",data);
                data.data['radio'] = payload.radio
                context.commit('login', data.data)
                // context.commit('addAuthRouter',null,{root:true})
                // context.commit({
                //     type:'addAuthRouter',
                //     // payload:
                // },{root:true})
                console.log("1212121212", data);
            }

            return data;
            // return
        }
    }
}